﻿using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace IQIGame.Onigao.Game.Editors
{
    /// <summary>
    /// 类名 : ugui的编辑功能扩展
    /// 作者 : Canyon / 龚阳辉
    /// 日期 : 2024-07-29 18:24
    /// 功能 : 
    /// </summary>
    public static class EditorUGUIExtensions
    {
        const int add_priority = 10000;
        const int menu_priority = int.MinValue + add_priority;

        static void SelectionComponent<T>(Action<T> funcEach) where T : Component
        {
            var gobj = Selection.activeGameObject;
            if (gobj == null || funcEach == null)
                return;
            var arrs = gobj.GetComponentsInChildren<T>(true);
            for (int i = 0; i < arrs.Length; i++)
            {
                funcEach(arrs[i]);
            }

            EditorUtility.SetDirty(gobj);
        }

        static void SelectionMaskableGraphic(Action<MaskableGraphic> funcEach)
        {
            SelectionComponent(funcEach);
        }

        [MenuItem("GameObject/UGUIEx/ClearRaycastTarget", false, menu_priority)]
        static void ClearRaycastTarget()
        {
            SelectionMaskableGraphic(maskableGraphic => maskableGraphic.raycastTarget = false);
        }

        [MenuItem("GameObject/UGUIEx/Maskable-True", false, menu_priority)]
        static void MaskableTrue()
        {
            SelectionMaskableGraphic(maskableGraphic => maskableGraphic.maskable = true);
        }

        static void SelectionTransform(Action<Transform> funcEach)
        {
            SelectionComponent(funcEach);
        }

        [MenuItem("GameObject/UGUIEx/DefaultLocalScaleAndRotation", false, menu_priority)]
        static void DefaultLocalScaleAndRotation()
        {
            SelectionTransform(transform =>
            {
                transform.localEulerAngles = Vector3.zero;
                transform.localScale = Vector3.one;
            });
        }
    }
}
